class CompilationController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [compilationInstanceList: Compilation.list(params), compilationInstanceTotal: Compilation.count()]
    }

    def create = {
        def compilationInstance = new Compilation()
        compilationInstance.properties = params
        return [compilationInstance: compilationInstance]
    }

    def save = {
        def compilationInstance = new Compilation(params)
        if (!compilationInstance.hasErrors() && compilationInstance.save()) {
            flash.message = "compilation.created"
            flash.args = [compilationInstance.id]
            flash.defaultMessage = "Compilation ${compilationInstance.id} created"
            redirect(action: "show", id: compilationInstance.id)
        }
        else {
            render(view: "create", model: [compilationInstance: compilationInstance])
        }
    }

    def show = {
        def compilationInstance = Compilation.get(params.id)
        if (!compilationInstance) {
            flash.message = "compilation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Compilation not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [compilationInstance: compilationInstance]
        }
    }

    def edit = {
        def compilationInstance = Compilation.get(params.id)
        if (!compilationInstance) {
            flash.message = "compilation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Compilation not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [compilationInstance: compilationInstance]
        }
    }

    /*def enable = {
    	params.disabled = false
    	comicService.update(params)
    }
    
    def disable = {
    	params.disabled = true
    	comicService.update(params)
    }*/
    
    def update = {
        def compilationInstance = Compilation.get(params.id)
        if (compilationInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (compilationInstance.version > version) {
                    
                    compilationInstance.errors.rejectValue("version", "compilation.optimistic.locking.failure", "Another user has updated this Compilation while you were editing")
                    render(view: "edit", model: [compilationInstance: compilationInstance])
                    return
                }
            }
            compilationInstance.properties = params
            if (!compilationInstance.hasErrors() && compilationInstance.save()) {
                flash.message = "compilation.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Compilation ${params.id} updated"
                redirect(action: "show", id: compilationInstance.id)
            }
            else {
                render(view: "edit", model: [compilationInstance: compilationInstance])
            }
        }
        else {
            flash.message = "compilation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Compilation not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def compilationInstance = Compilation.get(params.id)
        if (compilationInstance) {
            try {
                compilationInstance.delete()
                flash.message = "compilation.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Compilation ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "compilation.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Compilation ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "compilation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Compilation not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
